package avicit.bdp.dcs.plugin;

import avicit.platform6.core.exception.BusinessException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2021-01-27 17:32
 * @类说明：构建DataxJson的工具类
 * @修改记录：
 */
public class DataxJsonUtil {

    private DataxJsonInterface readerPlugin;

    private DataxJsonInterface writerPlugin;

    private BaseDataxParam readerParam;

    private BaseDataxParam writerParam;

    private List<TransformerParam> transformerParamList;

    private Integer recordLimit;
    private Integer byteLimit;
    private Integer errorLimitRecord;

    /**
     * 初始化Reader插件
     */
    public void initReader(String dbType, BaseDataxParam readerParam) throws Exception {
        this.readerParam = readerParam;
        String classPath = PluginPropertyUtil.getString("reader." + dbType);
        Class c = Class.forName(classPath);
        if(c == null){
            throw new BusinessException(classPath + "路径对应的class不存在");
        }
        readerPlugin = (DataxJsonInterface) c.newInstance();
    }

    /**
     * 初始化Writer插件
     */
    public void initWriter(String dbType, BaseDataxParam writerParam) throws Exception {
        this.writerParam = writerParam;
        String classPath = PluginPropertyUtil.getString("writer." + dbType);
        Class c = Class.forName(classPath);
        if(c == null){
            throw new BusinessException(classPath + "路径对应的class不存在");
        }
        writerPlugin = (DataxJsonInterface) c.newInstance();
    }

    /**
     * 初始化Transformer
     * @param transformerParamList
     */
    public void initTransformer(List<TransformerParam> transformerParamList) {
        this.transformerParamList = transformerParamList;
    }

    /**
     * 构建Job属性
     * @return
     */
    public Map<String, Object> buildJob() {
        Map<String, Object> res = new HashMap<>(4);
        Map<String, Object> jobMap = new HashMap<>(4);
        jobMap.put(DataxConstants.SETTING, buildSetting());
        jobMap.put(DataxConstants.CONTENT, ImmutableList.of(buildContent()));
        res.put(DataxConstants.JOB, jobMap);
        return res;
    }

    /**
     * 构建Setting属性
     * @return
     */
    public Map<String, Object> buildSetting() {
        Map<String, Object> res = new HashMap<>(4);
        Map<String, Object> speedMap = new HashMap<>(4);
        Map<String, Object> errorLimitMap = new HashMap<>(4);

        if(this.getRecordLimit() != null){
            speedMap.put(DataxConstants.RECORD, this.getRecordLimit());
        }
        if(this.getByteLimit() != null){
            speedMap.put(DataxConstants.BYTE, this.getByteLimit());
        }

        if(this.getRecordLimit()==null && this.getByteLimit()==null){
            speedMap.putAll(ImmutableMap.of(DataxConstants.CHANNEL, DataxConstants.CHANNEL_SIZE));
        }

        errorLimitMap.put(DataxConstants.PERCENTAGE, 0.02);
        if(this.getErrorLimitRecord() != null){
            errorLimitMap.put(DataxConstants.RECORD, this.getErrorLimitRecord());
        }else{
            errorLimitMap.put(DataxConstants.RECORD, 0);
        }

//        errorLimitMap.putAll(ImmutableMap.of(DataxConstants.RECORD, 0, DataxConstants.PERCENTAGE, 0.02));
        res.put(DataxConstants.SPEED, speedMap);
        res.put(DataxConstants.ERROR_LIMIT, errorLimitMap);
        return res;
    }

    /**
     * 构建Content属性
     * @return
     */
    public Map<String, Object> buildContent() {
        Map<String, Object> res = new HashMap<>(4);
        res.put(DataxConstants.READER, readerPlugin.buildReader(readerParam));
        res.put(DataxConstants.WRITER, writerPlugin.buildWriter(writerParam));
        res.put(DataxConstants.TRANSFORMER, this.transformerParamList);
        return res;
    }

    public DataxJsonInterface getReaderPlugin() {
        return readerPlugin;
    }

    public void setReaderPlugin(DataxJsonInterface readerPlugin) {
        this.readerPlugin = readerPlugin;
    }

    public DataxJsonInterface getWriterPlugin() {
        return writerPlugin;
    }

    public void setWriterPlugin(DataxJsonInterface writerPlugin) {
        this.writerPlugin = writerPlugin;
    }

    public BaseDataxParam getReaderParam() {
        return readerParam;
    }

    public void setReaderParam(BaseDataxParam readerParam) {
        this.readerParam = readerParam;
    }

    public BaseDataxParam getWriterParam() {
        return writerParam;
    }

    public void setWriterParam(BaseDataxParam writerParam) {
        this.writerParam = writerParam;
    }

    public Integer getRecordLimit() {
        return recordLimit;
    }

    public void setRecordLimit(Integer recordLimit) {
        this.recordLimit = recordLimit;
    }

    public Integer getByteLimit() {
        return byteLimit;
    }

    public void setByteLimit(Integer byteLimit) {
        this.byteLimit = byteLimit;
    }

    public Integer getErrorLimitRecord() {
        return errorLimitRecord;
    }

    public void setErrorLimitRecord(Integer errorLimitRecord) {
        this.errorLimitRecord = errorLimitRecord;
    }

}
